package de.scs.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jfree.ui.RefineryUtilities;

import de.scs.constants.InsulinDoseConstants;

public class InsulinPumpMonitor extends JPanel implements ActionListener
{

  /**
   * 
   */
  private static final long             serialVersionUID           = 680448063013526361L;

  public static final String            TOTAL_DOSES_LABEL_PREFIX   = "Insulin capacity: ";
  public static final String            TOTAL_DOSES_LABEL_SUFFIX   = " / " + InsulinDoseConstants.INSULIN_CAPACITY
                                                                     + "   I.U.";
  public static final String            INJECTED_DOSE_LABEL_PREFIX = "Amount of injected insulin: ";
  public static final String            INJECTED_DOSE_LABEL_SUFFIX = "   I.U.";
  private GridBagConstraints            constraints;
  private static Properties             props                      = new Properties();
  private static Map<String, Component> monitorComponents          = new HashMap<String, Component>();

  private static BloodSugarStatisticsGraph     graph;

  public InsulinPumpMonitor()
  {
    setLayout(new GridBagLayout());
    setPreferredSize(new Dimension(450, 420));
    constraints = new GridBagConstraints();
    try
    {
      props.load(getClass().getResourceAsStream("/resources/constants/constants.properties"));
    }
    catch (IOException e)
    {
      System.err.println("couldn't load properties file.");
    }
    graph = new BloodSugarStatisticsGraph("Blood Sugar Statistics Graph");
    graph.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    graph.addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        graph.setVisible(false);
      }
    });
    graph.pack();
    RefineryUtilities.centerFrameOnScreen(graph);
    createMonitorUI();
  }

  private void createMonitorUI()
  {
    constraints.insets = new Insets(0, 25, 0, 25);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.ipadx = 0;
    constraints.weightx = 0.5;
    constraints.gridx = 0;
    constraints.gridy = 0;
    add(new JLabel("Battery Status"), constraints);
    constraints.weightx = 0.5;
    constraints.gridx = 1;
    constraints.gridy = 0;
    add(new JLabel("Insulin Status"), constraints);
    constraints.insets = new Insets(5, 40, 5, 0);
    constraints.anchor = GridBagConstraints.LINE_END;
    constraints.weightx = 0.5;
    constraints.gridx = 2;
    constraints.gridy = 0;
    add(new JLabel("Timer"), constraints);
    constraints.insets = new Insets(0, 10, 0, 10);
    constraints.gridx = 0;
    constraints.gridy = 1;
    JLabel batteryStatusLabel = new JLabel(Utils.scaleImage(
      new ImageIcon(getClass().getResource(props.getProperty("FULL_BATTERY"))), 30, 30));
    monitorComponents.put("batterystatus", batteryStatusLabel);
    add(batteryStatusLabel, constraints);
    constraints.gridx = 1;
    constraints.gridy = 1;
    JLabel insulinStatusLabel = new JLabel(Utils.scaleImage(
      new ImageIcon(getClass().getResource(props.getProperty("FULL_INSULIN"))), 30, 30));
    monitorComponents.put("insulinstatus", insulinStatusLabel);
    add(insulinStatusLabel, constraints);
    constraints.gridx = 2;
    constraints.gridy = 1;
    JLabel timeLabel = new JLabel("00:00:00");
    monitorComponents.put("timer", timeLabel);
    timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    timeLabel.setPreferredSize(new Dimension(100, 50));
    timeLabel.setFont(new Font("SansSerif", Font.ITALIC, 17));
    timeLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    add(timeLabel, constraints);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 5, 5);
    constraints.gridwidth = 3;
    constraints.gridx = 0;
    constraints.gridy = 2;
    add(new JSeparator(), constraints);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.insets = new Insets(40, 10, 10, 10);
    add(Utils.defineFont(new JLabel("Blood Sugar Level:")), constraints);
    constraints.gridx = 1;
    constraints.gridy = 3;
    JLabel bslStat = new JLabel("Normal");
    bslStat.setForeground(Utils.setHSBColor(120, 61, 77));
    monitorComponents.put("bloodsugarstatus", bslStat);
    add(Utils.defineFont(bslStat), constraints);
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridheight = 2;
    constraints.gridx = 2;
    constraints.gridy = 3;
    JPanel severityPanel = new JPanel();
    JLabel severityLabel = new JLabel(Utils.scaleImage(
      new ImageIcon(getClass().getResource(props.getProperty("NORMAL_SEVERITY"))), 120, 60));
    severityLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    monitorComponents.put("severity", severityLabel);
    severityPanel.add(severityLabel);
    add(severityPanel, constraints);
    constraints.gridx = 0;
    constraints.gridy = 4;
    constraints.insets = new Insets(0, 10, 0, 10);
    add(Utils.defineFont(new JLabel("Hardware failure:")), constraints);
    constraints.gridx = 1;
    constraints.gridy = 4;
    JLabel hardStat = new JLabel("No failure detected");
    monitorComponents.put("hardwarefailure", hardStat);
    hardStat.setForeground(Utils.setHSBColor(120, 61, 77));
    add(Utils.defineFont(hardStat), constraints);
    JPanel insulinStatisticPanel = new JPanel(new FlowLayout(SwingConstants.TRAILING, 15, 15));
    insulinStatisticPanel.setBorder(BorderFactory.createEtchedBorder());
    constraints.insets = new Insets(20, 10, 20, 10);
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridheight = 2;
    constraints.gridwidth = 3;
    constraints.gridx = 0;
    constraints.gridy = 5;
    constraints.ipadx = 0;
    constraints.ipady = 40;
    JLabel injectedDoseLabel = new JLabel(INJECTED_DOSE_LABEL_PREFIX + "0" + INJECTED_DOSE_LABEL_SUFFIX);
    monitorComponents.put("injecteddose", injectedDoseLabel);
    injectedDoseLabel.setPreferredSize(new Dimension(300, 20));
    insulinStatisticPanel.add(injectedDoseLabel);
    JLabel totalDosesLabel = new JLabel(TOTAL_DOSES_LABEL_PREFIX + "0" + TOTAL_DOSES_LABEL_SUFFIX);
    monitorComponents.put("totaldoses", totalDosesLabel);
    insulinStatisticPanel.add(totalDosesLabel);
    add(Utils.defineFont(insulinStatisticPanel), constraints);
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.ipady = 10;
    constraints.gridx = 0;
    constraints.gridy = 7;
    constraints.insets = new Insets(0, 150, 0, 150);
    JButton graphButton = new JButton("show graph");
    graphButton.addActionListener(this);
    graphButton.setPreferredSize(new Dimension(100, 30));
    add(graphButton, constraints);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 5);
    constraints.ipady = 0;
    constraints.gridwidth = 3;
    constraints.gridheight = 0;
    constraints.gridx = 0;
    constraints.gridy = 8;
    add(new JSeparator(), constraints);
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 0;
    constraints.gridy = 11;
    constraints.insets = new Insets(20, 5, 0, 5);
    JLabel statusField = new JLabel("No Activities");
    monitorComponents.put("statusfield", statusField);
    JTextField statusColor = new JTextField(1);
    statusColor.setBackground(Color.GRAY);
    statusColor.setEditable(false);
    monitorComponents.put("statuscolor", statusColor);
    JPanel statusPanel = new JPanel(new FlowLayout(SwingConstants.WEST, 5, 2));
    statusPanel.add(statusColor);
    statusPanel.add(statusField);
    add(statusPanel, constraints);
  }

  public static Map<String, Component> getMonitorComponents()
  {
    return monitorComponents;
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    graph.setVisible(true);
  }

  public static BloodSugarStatisticsGraph getGraph()
  {
    return graph;
  }

}
